<?php
/**
 * Created by PhpStorm.
 * User: ivi
 * Date: 2019/2/19
 * Time: 22:49
 */

namespace App\Utils;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;

class Pager
{
    /**
     * 设置排序
     *
     * @param Builder $query
     * @param       $params
     * @param string $defaultSoftField
     * @param array $fieldMap
     * @return Builder
     */
    public static function setPageParams($query, $params, $defaultSoftField = 'id', $fieldMap = [])
    {
        self::setOrderByParams($query, $params, $defaultSoftField, $fieldMap);
        if (!Arr::get($params, 'export', 0)) {
            $limit = Arr::get($params, 'limit', 10);
            $page = Arr::get($params, 'page', 1);
            if ($page !== false) {
                $query->skip(($page - 1) * $limit)->take($limit);
            }
        }
        return $query;
    }

    /**
     * @param Builder $query
     * @param $params
     * @return Builder
     */
    public static function filter($query, $params){
        //////////////////解析请求筛选条件
        $wheres = Arr::get($params, 'wheres', '');
        if(!empty($wheres)){
            $wheres=json_decode($wheres,true);
            if(count($wheres)){
                foreach ($wheres as $where){
                    $operator=$where['operator'];
                    switch ($operator){
                        case 'like':
                            $query->where($where['field'],'like','%'.$where['value'].'%');
                            break;
                        case 'is_null':
                            $query->whereNull($where['field']);
                        case 'is_not_null':
                            $query->whereNotNull($where['field']);
                            break;
                        case 'like':
                            $query->where($where['field'],'like','%'.$where['value'].'%');
                            break;
                        default:
                            $query->where($where['field'],$operator,$where['value']);
                            break;
                    }
                }
            }
        }
        //////////////////解析完成
        return $query;
    }

    /**
     * 设置排序
     *
     * @param   Builder $query
     * @param       $params
     * @param string $defaultSoftField
     * @param array $fieldMap
     * @return Builder
     */
    public static function setOrderByParams($query, $params, $defaultSoftField = 'id', $fieldMap = [])
    {
        $orders = Arr::get($params, 'orders', '');
        if(!empty($orders)){
            $orders=json_decode($orders,true);
        }else{
            $orders=[];
        }
        $orderMap=[];
        if(count($orders)){
            foreach ($orders as $order){
                $query->orderBy($order['field'], Arr::get($order, 'soft', 'desc'));
                $orderMap[$order['field']]=true;
            }
        }
        if (is_array($defaultSoftField)) {
            foreach ($defaultSoftField as $key => $item) {
                if (isset($orderMap[$key])) {
                    continue;
                }
                $query->orderBy($key, $item);
            }
        } else {
            if (!isset($orderMap[$defaultSoftField])) {
                $query->orderBy($defaultSoftField, 'desc');
            }
        }
        return $query;
    }
}
